package com.tekj.cxqcb.view.Module.activity;

import android.content.Intent;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.tekj.cxqcb.MainBActivity;
import com.tekj.cxqcb.R;
import com.tekj.cxqcb.operation.BusinessDao;
import com.tekj.cxqcb.operation.resultBean.SelectByIdBean;
import com.tekj.cxqcb.view.Module.activity.StoreModule.AdministrationActivity;
import com.tekj.cxqcb.view.Module.activity.StoreModule.ClassificationActivity;
import com.tekj.cxqcb.view.Module.activity.StoreModule.CommodityManagementActivity;
import com.tekj.cxqcb.view.Module.activity.StoreModule.HotWordsActivity;
import com.tekj.cxqcb.view.Module.activity.StoreModule.StoreDetailsActivity;

import butterknife.BindView;
import butterknife.OnClick;
import common.base.activity.BaseActivity;
import common.base.operationHelper.Bean.BindingViewBean;
import es.dmoral.toasty.Toasty;

/**
 * 新店铺管理
 */
public class NewStoreManagementActivity extends BaseActivity {
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_l)
    TextView tvL;
    @BindView(R.id.ll_back)
    LinearLayout llBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_r)
    ImageView ivR;
    @BindView(R.id.tv_r)
    TextView tvR;
    @BindView(R.id.ll_right)
    LinearLayout llRight;
    @BindView(R.id.rl_title_bg)
    RelativeLayout rlTitleBg;
    @BindView(R.id.tv_id)
    TextView tvId;
    @BindView(R.id.img_photo)
    ImageView imgPhoto;
    @BindView(R.id.tv_title_name)
    TextView tvTitleName;
    @BindView(R.id.tv_IsBusiness)
    TextView tvIsBusiness;
    @BindView(R.id.tv_number)
    TextView tvNumber;
    @BindView(R.id.tv_Fraction)
    TextView tvFraction;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.ll_Shop)
    LinearLayout llShop;
    @BindView(R.id.appbar)
    AppBarLayout appbar;
    @BindView(R.id.ll_mdzl)
    LinearLayout llMdzl;
    @BindView(R.id.ll_spgl)
    LinearLayout llSpgl;
    @BindView(R.id.ll_flgl)
    LinearLayout llFlgl;
    @BindView(R.id.ll_mdpj)
    LinearLayout llMdpj;
    @BindView(R.id.ll_rcpz)
    LinearLayout llRcpz;
    @BindView(R.id.coordinator)
    CoordinatorLayout coordinator;

    private BusinessDao businessDao;
    private SelectByIdBean.DataBeanX.DataBean data;
    private String[] shopStates = {"店铺未开启", "营业中", "歇业中", "已关闭"};

    @Override
    public void init() {
        tvTitle.setText("门店管理");
        businessDao = new BusinessDao(activity, this);
        businessDao.selectAll(MainBActivity.User.getUserId());
    }

    @Override
    public int setLayout() {
        return R.layout.activity_new_store_management;
    }


    boolean isFirst;

    @Override
    public void initLoadStart() {
        if (isFirst) {
            loading.show();
            isFirst = false;
        } else
            showDialog();
    }

    @Override
    public void initLoadEnd() {
        loading.dismiss();
        dialog.dismiss();
    }

    @Override
    public void initBindingView(Object mBean) {
        BindingViewBean bindingViewBean = (BindingViewBean) mBean;
        switch (bindingViewBean.getmEnumStatus()) {
            case 通过id查找店铺:
                SelectByIdBean byIdBean = (SelectByIdBean) bindingViewBean.getBean();
                if ("0000000".equals(byIdBean.getCode())) {
                    if (byIdBean.getData().getData() == null) {
                        startActivity(new Intent(activity, AdministrationActivity.class));
                        finish();
                    } else
                        setViewData(byIdBean.getData().getData());
                } else {
                    Toasty.normal(activity, byIdBean.getMsg()).show();
                }
                break;
        }
    }

    @OnClick({R.id.ll_back, R.id.ll_mdzl, R.id.ll_spgl, R.id.ll_flgl, R.id.ll_mdpj, R.id.ll_rcpz})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_back:
                finish();
                break;
            case R.id.ll_mdzl:
                Intent intent = new Intent(activity, StoreDetailsActivity.class);
                intent.putExtra("data", data);
                startActivity(intent);
                break;
            case R.id.ll_spgl:
                startActivity(new Intent(activity, CommodityManagementActivity.class));
                break;
            case R.id.ll_flgl:
                startActivity(new Intent(activity, ClassificationActivity.class));
                break;
            case R.id.ll_mdpj:
                Toasty.normal(activity, "评价功能开发中。。").show();
                break;
            case R.id.ll_rcpz:
                startActivity(new Intent(activity, HotWordsActivity.class));
                break;
        }
    }

    /**
     * 设置界面中店铺信息
     */
    private void setViewData(SelectByIdBean.DataBeanX.DataBean bean) {
        data = bean;
        if (bean.getShopImage().length > 0)
            Picasso.get().load(!bean.getShopImage()[0].equals("") ? bean.getShopImage()[0] : "1")
                    .into(imgPhoto);
        tvTitleName.setText(bean.getShopName());
        tvAddress.setText(bean.getAddress().replace(",", " "));
        if (bean.getShopState() != 0)
            tvIsBusiness.setText(shopStates[bean.getShopState()]);

        tvId.setText("服务门店ID：" + MainBActivity.User.getShopId());

    }


}
